package com.lanmon.controller.auth;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanmon.base.RequestResult;
import com.lanmon.entity.ComUser;
import com.lanmon.service.auth.ComStudentService;
import com.lanmon.service.auth.ComUserService;
import com.lanmon.util.Coder;

/**
 * 系统登录页控制器
 * @author resigshy
 *
 */
@Controller
@RequestMapping("/auth")
public class LoginController {
	@Autowired
	private ComUserService comUserService;
	
	@Autowired
	private ComStudentService comStudentService;
	
	@RequestMapping(method = RequestMethod.GET, value = "/loginpage")
	public String init() {
		return "/auth/login";
	}
	
	@RequestMapping("/loginAction")
	@ResponseBody
	public RequestResult loginAction(ComUser user, HttpServletRequest request) {
		RequestResult rr = new RequestResult();
		try {
			user.setPassword(Coder.md5Pass(user.getPassword()));
			user = comUserService.findlogin(user);
			if (user != null) {
				request.getSession().setAttribute("user", user);
				//return "redirect:/index";// redirect方式跳转
				rr.setResultUrl("/index");
				return rr;
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		rr.setSuccess(false);
		rr.setMessage("登录失败，用户名或者密码错误");
		return rr;
	}
	
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request ){
		request.getSession().removeAttribute("user");
		request.getSession().invalidate();
		return "redirect:/auth/loginpage";
	}
	
}
